//
//  NKNetWorkTool.m
//  NekaPrice
//
//  Created by yu on 2018/6/11.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "NKNetWorkTool.h"

@implementation NKNetWorkTool


+ (instancetype)sharedTool {
    static NKNetWorkTool *_networkTool;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _networkTool = [super manager];
        _networkTool.responseSerializer = [AFHTTPResponseSerializer serializer];
        _networkTool.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
//        AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
//        //10秒超时
////        requestSerializer.timeoutInterval = 100;
//        _networkTool.requestSerializer = requestSerializer;
//
//        //获取AFN原有的数据解析类型 然后新增一些响应解析器能够接受的数据类型
//        NSMutableSet *acceptableContentTypes = [NSMutableSet setWithSet:_networkTool.responseSerializer.acceptableContentTypes];
//        NSLog(@"%@", acceptableContentTypes);
//        [acceptableContentTypes addObjectsFromArray:@[@"application/json", @"text/json", @"text/javascript",@"text/html",@"plant/html",@"text/plain",@"text/xml"]];
//        _networkTool.responseSerializer.acceptableContentTypes = acceptableContentTypes;
    });
    return _networkTool;
}

#pragma mark -- 自定义GET
- (void)requestGET:(NSString *)URLString parames:(id)parames success:(void (^)(id responseObj))success failure:(void (^)(id error))failure {
    //AFN没有做UTF8转码 防止URL字符串中含有中文或特殊字符发生崩溃

    URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [self GET:URLString parameters:parames progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"%@", str);
        NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        success(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
    
}

#pragma mark -- 自定义POST
- (void)requestPOST:(NSString *)URLString parames:(id)parames success:(void (^)(id responseObj))success failure:(void (^)(id error))failure {
    URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self POST:URLString parameters:parames progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}




@end
